package com.allin.mall.user.controller;

import com.allin.mall.common.core.api.CommonResult;
import com.allin.mall.common.redis.annotation.DistributedLock;
import com.allin.mall.common.redis.service.RedisService;
import com.allin.mall.user.entity.User;
import com.allin.mall.user.service.TimerService;
import com.allin.mall.user.service.UserService;
import com.allin.mall.user.timer.timertask.MallTimerTask;
import io.netty.util.HashedWheelTimer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequestMapping("/test")
@RestController
public class TestController {

    @Resource
    private TimerService timerService;
    @Resource
    private UserService service;

    @Resource
    private RedisService redisService;

    @GetMapping
    public CommonResult testBody(@RequestBody(required = false) Map<String, String> body) {
        service.testLock();
        return CommonResult.success(body);
    }

    @GetMapping("/testMset")
    public CommonResult testMSet() {
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 10000; i++) {
            map.put("{key12}:" + i, "value" + i);
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("mset");
        redisService.mset(map, TimeUnit.HOURS, 2);
        stopWatch.stop();
        stopWatch.start("循环");
        map.forEach(
                (key, value) -> {redisService.set(key, value, 10000);}
        );
        stopWatch.stop();
        log.info(stopWatch.prettyPrint());
        return CommonResult.success();
    }

    @GetMapping("/wheelTime")
    public CommonResult testWheelTime(@RequestParam(required = false) String testParam, @RequestBody(required = false) Map<String, String> body) {
        System.out.println("body = " + body);
        timerService.addTimerTask(new MallTimerTask());
        return CommonResult.success();
    }

    @DistributedLock(leaseTime = 10, key = "#uid")
    @GetMapping("/gson/adapter/{uid}")
    public CommonResult<User> getUser(@PathVariable Long uid) {
        return CommonResult.success(service.getUserById(uid));
    }

//    consumes = "text/plain"
    @PostMapping(value = "/gson/adapter", consumes = MediaType.TEXT_PLAIN_VALUE)
    public CommonResult addUser(@RequestBody User user) {
        log.info(user.toString());
        return CommonResult.success();
    }
}
